//
//  LoginVCViewController.m
//  001--RAC集合类
//
//  Created by 王苏进 on 3/5/17.
//  Copyright © 2017年 TZ. All rights reserved.
//

#import "LoginVCViewController.h"
#import "LoginVM.h"
#import <ReactiveObjC.h>

@interface LoginVCViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf1;
@property (weak, nonatomic) IBOutlet UIButton *logBtn;

@property (weak, nonatomic) IBOutlet UITextField *tf2;


@property (nonatomic, strong)LoginVM *vm;

@end

@implementation LoginVCViewController



- (LoginVM *)vm{
    if(!_vm){
        _vm = [LoginVM new];
    }
    return _vm;
}





- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor redColor];
    
    
    //绑定vm的账号 密码
    RAC(self.vm, account) = _tf1.rac_textSignal;
    RAC(self.vm, pwd) = _tf2.rac_textSignal;
    RAC(_logBtn,enabled) = self.vm.loginEnableSignal;
    [[_logBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.vm.loginCommand execute:@1];
    }];;
    [self.vm.loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
}




- (void)test1{
    //组合信号（无序组合）
    RAC(_logBtn,enabled) = [RACSignal combineLatest:@[_tf1.rac_textSignal,_tf2.rac_textSignal] reduce:^id _Nullable(NSString * account, NSString *pwd){
        
        return @(account.length && pwd.length);
    }];

    //创建命令
    // command
    
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        return   [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
  
            //密码加密  发送请求 获取登录请求
            [subscriber sendNext:@"登录结果"];
            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^{
                
            }];
            
        }];
        
    }];
    
    [[_logBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [command execute:@1];
        
    }];
    
    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"%@", x);
        
    }];
    
  
}

@end
